<template>
  <el-dialog
    title="修改文章"
    :visible.sync="showDialog"
    width="1000px"
    height="400px"
    center
  >
    <!-- 表单内容 -->
    <el-form ref="form" label-width="120px" :rules="rules" :model="form">
      <el-form-item label="文章标题" prop="title">
        <el-input v-model="form.title" style="width: 660px" size="mini" />
      </el-form-item>
      <el-form-item label="文章内容" prop="articleBody">
        <quillEditor
          style="width: 658px; height: 180px"
          v-model="form.articleBody"
        ></quillEditor>
      </el-form-item>
      <br />
      <br />
      <br />
      <br />
      <br />

      <el-form-item label="视频地址" prop="videoURL">
        <el-input v-model="form.videoURL" style="width: 660px" size="mini" />
      </el-form-item>
      <el-form-item>
        <el-row type="flex" justify="center">
          <el-col :span="12">
            <el-button type="primary" size="mini" @click="submit"
              >确定</el-button
            >
            <el-button size="mini" @click="cancel">取消</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script>
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor'
import { add, update } from '@/api/hmmm/articles.js'
export default {
  components: {
    quillEditor
  },
  props: {
    isUpdate: Boolean
  },
  data () {
    return {
      showDialog: false,
      rules: {
        title: [
          { required: true, message: '文章标题不能为空', trigger: 'blur' }
        ],
        articleBody: [
          { required: true, message: '文章内容不能为空', trigger: 'blur' }
        ],
        videoURL: [
          { required: true, message: '视频地址不能为空', trigger: 'blur' }
        ]
      },
      SubjectList: [],
      form: {
        title: '',
        articleBody: '',
        videoURL: '',
        id: null
      }
    }
  },
  methods: {
    async submit () {
      if (this.isUpdate) {
        try {
          await this.$refs.form.validate()
          await update(this.form)
          this.$message.success('更新成功!')
          this.cancel()
          this.$emit('getSubjectList')
        } catch (error) {
          console.log(error)
        }
      } else {
        try {
          await this.$refs.form.validate()
          await add(this.form)
          this.$message.success('添加成功!')
          this.cancel()
          this.$emit('getSubjectList')
        } catch (error) {
          console.log(error)
        }
      }
    },
    cancel () {
      this.$refs.form.resetFields()
      this.showDialog = false
    }
  },
  async created () {
    this.getSubjectList()
  }
}
</script>

<style scoped lang="less"></style>
